using NanoTimes
if VERSION < v"0.7.0-DEV.2004"
    using Base.Test
else
    using Test
end


# the present time, given as UT and as Locally
# changing time zone does not involve passage of time
@test abs( UniversalTime() - LocalTime() ) <= Seconds(1)

datetime1 = DateTime("2010-03-06T11:00")
datetime2 = DateTime("2012-04-05T20:30:40")
time1     = Time24(datetime1)
time2     = Time24(datetime2)
date1     = Date(datetime1)
date2     = Date(datetime2)

unv_time1  = UniversalTime(datetime1)
unv_time2  = UniversalTime(datetime2)
lcl_time1  = LocalTime(datetime1)
lcl_time2  = LocalTime(datetime2)

@test unv_time2 - unv_time1 == TimeSpan(Nanoseconds(nanoseconds(datetime2 - datetime1)))
@test milliseconds(lcl_time2 - lcl_time1) == milliseconds(datetime2 - datetime1)
@test Seconds(unv_time2 - unv_time1) == Seconds(Duration(datetime2 - datetime1))


ddate = date2 - date1
ddatetime = datetime2 - datetime1
dtime = time2 - time1

ndate1 = UniversalTime(date1)
ndate2 = UniversalTime(date2)
ndatetime1 = LocalTime(datetime1)
ndatetime2 = LocalTime(datetime2)
ndatetime3 = LocalTime(datetime2) - Day(12) + Milliseconds(98765) - Microsecond(123) - Nanosecond(2)

nspan1 = Duration(time1)
nspan2 = Duration(time2)

dndatetime = ndatetime2 - ndatetime1
dndate = ndate2 - ndate1
dnspan = nspan2 - nspan1

@test DateTime(date1) - dtime == DateTime(ndate1 - dnspan)
@test DateTime(date1) + dtime == DateTime(ndate1 + dnspan)

@test Milliseconds(dndatetime) == Milliseconds(ddatetime)
@test Days(dndate) == Days(ddate)
@test nanoseconds(dnspan) == nanoseconds(time2 - time1)

@test ndate1 == parse(UniversalTime, string(ndate1))
@test ndatetime2 == parse(LocalTime, string(ndatetime2))

#@test nspan1 == parse(Duration, string(nspan1))
#@test -dnspan == -parse(Duration, string(dnspan))

@test nanoseconds(ndatetime3) == ndatetime3.value
@test microseconds(ndatetime3) == fld(ndatetime3.value, 1_000)
@test milliseconds(ndatetime3) == fld(ndatetime3.value, 1_000_000)
@test seconds(ndatetime3) == fld(milliseconds(ndatetime3), 1_000)
@test minutes(ndatetime3) == fld(seconds(ndatetime3), 60)
@test hours(ndatetime3) == fld(minutes(ndatetime3), 60)
@test days(ndatetime3) == fld(hours(ndatetime3), 24)
@test weeks(ndatetime3) == fld(days(ndatetime3), 7)

@test Nanoseconds(ndatetime3) == Nanoseconds(nanoseconds(ndatetime3))
@test Microseconds(ndatetime3) == Microseconds(microseconds(ndatetime3))
@test Milliseconds(ndatetime3) == Milliseconds(milliseconds(ndatetime3))
@test Seconds(ndatetime3) == Seconds(seconds(ndatetime3))
@test Minutes(ndatetime3) == Minutes(minutes(ndatetime3))
@test Hours(ndatetime3) == Hours(hours(ndatetime3))
@test Days(ndatetime3) == Days(days(ndatetime3))
@test Weeks(ndatetime3) == Weeks(weeks(ndatetime3))

@test Years(3) - Hours(6) == TimeSpan(-nanoseconds(Hours(6)), months(Years(3)))

@test nspan1 == div((3 * nspan1), 3)
@test nspan1 == cld((5 * nspan1), 5)
@test nspan1 == fld((7 * nspan1), 7)
@test -nspan1 == div((-3 * nspan1), 3)
@test -nspan1 == cld((-5 * nspan1), 5)
@test -nspan1 == fld((-7 * nspan1), 7)

#time = Time(year=2017, month=1, day=2, hour=12, minute=34, second=56, millisecond=765, microsecond=432, nanosecond=100)
univtime = UniversalTime(2017, 1, 2, 12, 34, 56, 765, 432, 100)
localtime = LocalTime(2017, 1, 2, 12, 34, 56, 765, 432, 100)

@test parse(UniversalTime, "2017-01-02T12:34:56.7654321") == univtime
@test parse(LocalTime, "2017-01-02T12:34:56.7654321") == localtime

@test parse(UniversalTime, "20170102 12:34:56.7654321") == univtime
@test parse(LocalTime, "20170102 12:34:56.7654321") == localtime

@test parse(UniversalTime, "170102 12:34:56.7654321") == univtime
@test parse(LocalTime, "170102 12:34:56.7654321") == localtime

#span =day=2, hour=12, minute=34, second=56, millisecond=765, microsecond=432, nanosecond=100)
duration = Duration(2, 12, 34, 56, 765, 432, 100)
@test parse(Duration, string(duration)) == duration
@test duration == Duration(2dy + 12hr + 34mi + 56sc + 765ms + 432us + 100ns)
@test Nanoseconds(duration) == Days(2) + Hours(12) + Minutes(34) + Seconds(56) + Nanoseconds(765432100)

#timespan = TimeSpan(year=3, month=5, day=2, hour=12, minute=34, second=56, millisecond=765, microsecond=432, nanosecond=100)
timespan = TimeSpan(3, 5, 2, 12, 34, 56, 765, 432, 100)
@test isostring(timespan) == "P3Y5M2D12H34M56.765432100S"
@test parse(TimeSpan, isostring(timespan)) == timespan
@test string(timespan) == "+3ʸ5ᵐ2ᵈ12ʰ34ᵐ56.765432100ˢ"
@test parse(TimeSpan, isostring(timespan)) == timespan

